mod router;
mod handles;
mod state;
mod reply;
mod middlewares;
mod models;

use std::{env, io::Result};
use dotenv::dotenv;
use poem::{EndpointExt, middleware::Tracing, listener::TcpListener, Server};
use state::State;
use self::router::router;


#[tokio::main]
async fn main() -> Result<()> {
    dotenv().expect("No `.env` in current directory or parents");
    let db_url = env::var("DATABASE_URL").expect("No `DATABASE_URL`");
    let server_url = env::var("SERVER_URL").expect("No `SERVER_URL`");

    // tracing-subscriber 的 env-filter 特性，
    // 令 poem 打 debug 级别的日志
    env::set_var("RUST_LOG", "poem=debug");
    tracing_subscriber::fmt::init();

    let db = sea_orm::Database::connect(&db_url).await.unwrap();
    let state = State::new(db);
    let app = router().with(Tracing).data(state);
    let server = Server::new(TcpListener::bind(server_url)).name("personnel-manager");

    server.run(app).await
}
